//
//  MNDInvestmentDetailHeaderView.m
//  minidai
//
//  Created by Steven.Yung on 2017/5/15.
//  Copyright © 2017年 迷你金服. All rights reserved.
//

#import "MNDInvestmentDetailHeaderView.h"
#import "UIImageView+JKAddition.h"

@interface MNDInvestmentDetailHeaderView ()

@property (strong, nonatomic) UIButton *expandButton;
@property (strong, nonatomic) UIImageView *expandImage;

@end

@implementation MNDInvestmentDetailHeaderView

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithReuseIdentifier:reuseIdentifier];
    
    if (self) {
        [self initialContents];
        self.contentView.backgroundColor = [UIColor whiteColor];
    }
    
    return self;
}



- (void)expandAction:(UIButton *)sender {
    self.model.isSectionExpand = !self.model.isSectionExpand;
    
    [UIView animateWithDuration:0.3 animations:^{
        
        if (self.model.isSectionExpand) {
            
            self.expandImage.transform = CGAffineTransformIdentity;
            
        }else{
            
            self.expandImage.transform = CGAffineTransformMakeRotation(M_PI);
        }
        
    }];
    
    if (self.headerClickedBack) {
        self.headerClickedBack(self.model.isSectionExpand);
    }
}

- (void)setModel:(MNDInvestmentSectionModel *)model {
    _model = model;
    
    [UIView animateWithDuration:0.3 animations:^{
        
        if (self.model.isSectionExpand) {
            
            self.expandImage.transform = CGAffineTransformIdentity;
            
        }else{
            
            self.expandImage.transform = CGAffineTransformMakeRotation(M_PI);
        }
        
    }];
}

- (void)initialContents {
    CGFloat edgeMargin = 15.0;
    [self.deadlineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.contentView).offset(edgeMargin);
        make.bottom.equalTo(self.contentView.mas_centerY).offset(3.0);
    }];
    [self.returnStatusLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.deadlineLabel);
        make.top.equalTo(self.deadlineLabel.mas_bottom).offset(-2);
    }];
    [self.expandImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(self.contentView).offset(-edgeMargin);
        make.centerY.equalTo(self.contentView);
    }];
    [self.returnMoneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(self.expandImage.mas_leading).offset(-8.0);
        make.centerY.equalTo(self.expandImage);
    }];

    [self.expandButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView);
    }];
//    if (self.isExpand) {
//        [self expandAction:self.expandButton];
//    }
}

- (UILabel *)deadlineLabel {
    if (!_deadlineLabel) {
        _deadlineLabel = [self labelWithDefaultText:@"第一期" textColor:kColorBlack fontSize:14.0];
        [self.contentView addSubview:_deadlineLabel];
    }
    
    return _deadlineLabel;
}

- (UILabel *)returnStatusLabel {
    if (!_returnStatusLabel) {
        _returnStatusLabel = [self labelWithDefaultText:@"未回款" textColor:kColorOrange fontSize:12.0];
        [self.contentView addSubview:_returnStatusLabel];
    }
    
    return _returnStatusLabel;
}

- (UILabel *)returnMoneyLabel {
    if (!_returnMoneyLabel) {
        _returnMoneyLabel = [self labelWithDefaultText:@"0.00" textColor:kColorOrange fontSize:21.0];
        [self.contentView addSubview:_returnMoneyLabel];
    }
    
    return _returnMoneyLabel;
}

- (UIImageView *)expandImage {
    if (!_expandImage) {
        _expandImage = [UIImageView jk_imageViewWithImageNamed:@"investment_detail_expand"];
        
        [self.contentView addSubview:_expandImage];
    }
    
    return _expandImage;
}

- (UIButton *)expandButton {
    if (!_expandButton) {
        _expandButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_expandButton addTarget:self action:@selector(expandAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_expandButton];
    }
    
    return _expandButton;
}

- (UILabel *)labelWithDefaultText:(NSString *)text textColor:(UIColor *)color fontSize:(CGFloat)size {
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
    label.text = text;
    label.textColor = color;
    label.font = [UIFont fontWithName:@"PingFangSC-Regular" size:size];
    
    return label;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
